package com.yunc.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.modules.sys.entity.SysUserDept;
import com.yunc.modules.sys.mapper.SysUserDeptMapper;
import com.yunc.modules.sys.service.ISysUserDeptService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户多部门
 *
 * @author congcong tao
 * @create 2020/1/8 17:25
 */
@Service("sysUserDeptService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptMapper, SysUserDept> implements ISysUserDeptService {

    private final SysUserDeptMapper sysUserDeptMapper;

    @Override
    public boolean insertList(String userId, String deptIds) {
        if (StringUtils.isNotEmpty(deptIds)) {
            String[] roleIdArr = deptIds.split(",");
            if (roleIdArr != null && roleIdArr.length > 0) {
                List<SysUserDept> sysUserDeptList = new ArrayList<>();
                for (String deptId : roleIdArr) {
                    SysUserDept userDept = new SysUserDept();
                    userDept.setUserId(userId);
                    userDept.setDeptId(deptId);
                    sysUserDeptList.add(userDept);
                }
                boolean flag = this.saveBatch(sysUserDeptList);
                if (!flag) {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public List<String> listDept(String userId) {
        return sysUserDeptMapper.listDept(userId);
    }

}
